package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SalesVolumes;
import com.ruoyi.system.service.ISalesVolumesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售数量Controller
 *
 * @author ruoyi
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/system/volumes")
public class SalesVolumesController extends BaseController {
    @Autowired
    private ISalesVolumesService salesVolumesService;

    /**
     * 查询销售数量列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SalesVolumes salesVolumes) {
        startPage();
        List<SalesVolumes> list = salesVolumesService.selectSalesVolumesList(salesVolumes);
        return getDataTable(list);
    }

    /**
     * 获取销售数量详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(salesVolumesService.selectSalesVolumesById(id));
    }

    /**
     * 新增销售数量
     */
    @PostMapping
    public AjaxResult add(@RequestBody SalesVolumes salesVolumes) {
        SalesVolumes res = new SalesVolumes();
        res.setName(salesVolumes.getName());
        List<SalesVolumes> list = salesVolumesService.selectSalesVolumesList(res);
        Assert.isTrue(list.size()==0,"该农产品已有销量");
        salesVolumes.setCreateUser(SecurityUtils.getUserId().intValue());
        return toAjax(salesVolumesService.insertSalesVolumes(salesVolumes));
    }

    /**
     * 修改销售数量
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SalesVolumes salesVolumes) {
        salesVolumes.setUpdateUser(SecurityUtils.getUserId().intValue());
        return toAjax(salesVolumesService.updateSalesVolumes(salesVolumes));
    }

    /**
     * 删除销售数量
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(salesVolumesService.deleteSalesVolumesByIds(ids));
    }
}
